package trlygjj.tanruiliyigenjinjin.web.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import trlygjj.tanruiliyigenjinjin.domain.DataDictionary;
import trlygjj.tanruiliyigenjinjin.domain.DataDictionaryItem;
import trlygjj.tanruiliyigenjinjin.page.PageResult;
import trlygjj.tanruiliyigenjinjin.query.QueryObject;
import trlygjj.tanruiliyigenjinjin.service.IDataDictionaryService;
import trlygjj.tanruiliyigenjinjin.util.JsonResult;
import trlygjj.tanruiliyigenjinjin.util.PermissionName;

import java.util.List;

/**
 * Created by user on 2018/7/11.
 */
@Controller
@RequestMapping("dataDictionary1")
public class DataDictionaryController {

    @Autowired
    private IDataDictionaryService dataDictionaryService;

    /**
     * 提供一个默认不输入后缀,直接可以跳转到具有分页的列表页面
     * @return
     */
    @RequiresPermissions("dataDictionary1:index")
    @PermissionName("数据字典主页")
    @RequestMapping("")
    public String index(){
        return "dataDictionary1/list";
    }

    /**
     * 具有分页和高级查询
     * @return
     */
    @RequiresPermissions("dataDictionary1:list")
    @PermissionName("数据字典目录")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo){
        PageResult result = dataDictionaryService.query(qo);
        return result;
    }

    /**
     * 查询全部
     * @return
     */
    @RequiresPermissions("dataDictionary1:listAll")
    @PermissionName("数据字典全部数据")
    @RequestMapping("listAll")
    @ResponseBody
    public List<DataDictionary> listAll(){
        return dataDictionaryService.selectAll();
    }

    /**
     * 删除
     * @param dataDictionaryId 要删除的员工对象id
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequiresPermissions("dataDictionary1:delete")
    @PermissionName("数据字典删除")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long dataDictionaryId){
        JsonResult result = new JsonResult();
        try{
            dataDictionaryService.deleteByPrimaryKey(dataDictionaryId);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     *更新
     * @param dataDictionary 要更新的员工对象
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequiresPermissions("dataDictionary1:update")
    @PermissionName("数据字典更新")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(DataDictionary dataDictionary){
        JsonResult result = new JsonResult();
        try{
            dataDictionaryService.updateByPrimaryKey(dataDictionary);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     * 保存
     * @param dataDictionary 要保存的员工对象
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequiresPermissions("dataDictionary1:save")
    @PermissionName("数据字典保存")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(DataDictionary dataDictionary){
        JsonResult result = new JsonResult();
        try{
            dataDictionaryService.insert(dataDictionary);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    @RequestMapping("queryParentDictionary")
    @ResponseBody
    public List<DataDictionary> queryParentDictionary(){
        List<DataDictionary> dataDictionaries = dataDictionaryService.selectAll();
        return dataDictionaries;
    }
    /**
     * 根据数据字典的的sn查询明细
     * @param sn  数据字典的sn
     * @return 该字典对象的明细内容
     */
    @RequiresPermissions("dataDictionary1:selectDataDictionaryItemsBySn")
    @PermissionName("数据字典编码查询字典明细")
    @RequestMapping("selectDataDictionaryItemsBySn")
    @ResponseBody
    public List<DataDictionaryItem> selectDataDictionaryItemsBySn(String sn){
        DataDictionary dataDictionary=  dataDictionaryService.selectDataDictionaryItemsBySn(sn);
        List<DataDictionaryItem> items = dataDictionary.getItems();
        return items;
    }
}
